package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.VibrateUtils;

import java.util.Stack;

import flc.ast.BaseAc;
import flc.ast.CalSpeaker;
import flc.ast.R;
import flc.ast.databinding.ActivityRelationCalculateBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.base.StkRelationshipLocale;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.bean.StkRelationshipBean;

public class RelationCalculateActivity extends BaseAc<ActivityRelationCalculateBinding> {

    private Stack<String> historyStack; // 栈，用于存储点击历史
    private int mGender = 1;//本人性别：0表示女性,1表示男性
    private boolean mReverser = false;//称呼方式：true对方称呼我,false我称呼对方
    public boolean hasMan;
    private String[] mRelationArray;
    private boolean mHasShake;
    private boolean mHasVoice;
    private CalSpeaker speaker;

    @Override
    protected int onCreate() {
        return R.layout.activity_relation_calculate;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivRelationCalculateBack.setOnClickListener(v -> onBackPressed());

        mHasShake = false;
        mHasVoice = false;
        historyStack = new Stack<>();
        speaker = new CalSpeaker();
        speaker.enabled(mHasVoice);
        mRelationArray = getResources().getStringArray(R.array.relation_arr);
        //设置按钮监听器按钮
        Button[] standardButtons = new Button[]{mDataBinding.btnBaba, mDataBinding.btnMama, mDataBinding.btnRemove, mDataBinding.btnClear, mDataBinding.btnGege, mDataBinding.btnJiejie, mDataBinding.btnDidi, mDataBinding.btnMeimei, mDataBinding.btnLaoGong, mDataBinding.btnLaoPo, mDataBinding.btnNver, mDataBinding.btnErzi, mDataBinding.btnShake, mDataBinding.btnVoice, mDataBinding.btnEqual};
        for (Button button : standardButtons) {
            button.setOnClickListener(this);
        }

        mDataBinding.tvContent.setMovementMethod(new ScrollingMovementMethod());
        mDataBinding.tvInput.setMovementMethod(new ScrollingMovementMethod());

        mDataBinding.rgCallWay.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbCallMe:
                        speaker.speak(getString(R.string.he_call_me_text));
                        mReverser = !mReverser;
                        break;
                    case R.id.rbCallTa:
                        speaker.speak(getString(R.string.i_call_he_text));
                        mReverser = !mReverser;
                        break;
                    default:
                        break;
                }
            }
        });
        mDataBinding.rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbMan:
                        speaker.speak(getString(R.string.man_text));
                        mGender = 1;
                        break;
                    case R.id.rbWoman:
                        speaker.speak(getString(R.string.woman_text));
                        mGender = 0;
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        if (!FastClickUtil.isFastClick()) {
            shakeClick();
            switch (v.getId()) {
                case R.id.btnClear:
                    speaker.speak(getString(R.string.clear_text1));
                    mDataBinding.tvInput.setText("");
                    mDataBinding.tvContent.setText("");
                    historyStack.clear();
                    mDataBinding.tvJiao.setVisibility(View.INVISIBLE);
                    mDataBinding.btnLaoGong.setTextColor(Color.parseColor("#000000"));
                    mDataBinding.btnLaoPo.setTextColor(Color.parseColor("#000000"));
                    mDataBinding.btnLaoGong.setBackgroundResource(R.drawable.shape_white_8dp_bg);
                    mDataBinding.btnLaoPo.setBackgroundResource(R.drawable.shape_white_8dp_bg);
                    break;
                case R.id.btnRemove:
                    speaker.speak(getString(R.string.back_press_text));
                    mDataBinding.tvJiao.setVisibility(View.INVISIBLE);
                    removeFromHistory();
                    break;
                case R.id.btnShake:
                    mHasShake = !mHasShake;
                    speaker.speak(mHasShake ? getString(R.string.has_shake_text) : getString(R.string.cancel_shake_text));
                    mDataBinding.btnShake.setSelected(mHasShake);
                    break;
                case R.id.btnVoice:
                    mHasVoice = !mHasVoice;
                    speaker.enabled(mHasVoice);
                    mDataBinding.btnVoice.setSelected(mHasVoice);
                    break;
                case R.id.btnEqual:
                    String content = mDataBinding.tvInput.getText().toString();
                    if (!TextUtils.isEmpty(content)) {
                        getData(content);
                    } else {
                        ToastUtils.showShort(R.string.no_input_relation_tips);
                    }
                    break;
                case R.id.btnBaba:
                    addRelation(mRelationArray[0]);
                    break;
                case R.id.btnMama:
                    addRelation(mRelationArray[1]);
                    break;
                case R.id.btnGege:
                    addRelation(mRelationArray[2]);
                    break;
                case R.id.btnJiejie:
                    addRelation(mRelationArray[3]);
                    break;
                case R.id.btnMeimei:
                    addRelation(mRelationArray[4]);
                    break;
                case R.id.btnDidi:
                    addRelation(mRelationArray[5]);
                    break;
                case R.id.btnLaoGong:
                    addRelation(mRelationArray[6]);
                    break;
                case R.id.btnLaoPo:
                    addRelation(mRelationArray[7]);
                    break;
                case R.id.btnNver:
                    addRelation(mRelationArray[8]);
                    break;
                case R.id.btnErzi:
                    addRelation(mRelationArray[9]);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void initData() {

    }

    // 将新点击的内容添加到历史栈中，并更新显示
    private void addToHistory(String relation) {
        if (historyStack.isEmpty()) {
            if (!TextUtils.isEmpty(relation)) {
                speaker.speak(relation);
            }
        } else {
            if (!TextUtils.isEmpty(relation)) {
                speaker.speak(getString(R.string.de_text) + relation);
            }
        }

        historyStack.push(relation);
        updateResult(relation);
    }

    private void updateResult(String name) {
        StringBuilder result = new StringBuilder();
        for (String relation : historyStack) {
            if (result.length() > 0) {
                result.append(getString(R.string.de_text));
            }
            result.append(relation);
        }
        mDataBinding.tvInput.setText(result.toString());
        if (!historyStack.isEmpty()) {
            lastDefGender();

        }
    }


    private void lastDefGender() {
        switch (historyStack.get(historyStack.size() - 1)) {
            case "父亲":
            case "丈夫":
            case "哥哥":
            case "弟弟":
            case "儿子":
                hasMan = true;
                mDataBinding.btnLaoGong.setTextColor(Color.parseColor("#662C2D33"));
                mDataBinding.btnLaoPo.setTextColor(Color.parseColor("#2C2D33"));
                mDataBinding.btnLaoGong.setBackgroundResource(R.drawable.shape_gray_8dp_bg);
                mDataBinding.btnLaoPo.setBackgroundResource(R.drawable.shape_white_8dp_bg);
                break;
            case "母亲":
            case "妻子":
            case "姐姐":
            case "妹妹":
            case "女儿":
                hasMan = false;
                mDataBinding.btnLaoPo.setTextColor(Color.parseColor("#662C2D33"));
                mDataBinding.btnLaoGong.setTextColor(Color.parseColor("#2C2D33"));
                mDataBinding.btnLaoPo.setBackgroundResource(R.drawable.shape_gray_8dp_bg);
                mDataBinding.btnLaoGong.setBackgroundResource(R.drawable.shape_white_8dp_bg);
                break;
            default:
                break;
        }
    }

    public void addRelation(String relation) {
        if (historyStack.size() < 12) {
            if (historyStack.isEmpty()) {
                addToHistory(relation);
            } else {
                if (hasMan) {
                    //男，不能选择添加夫
                    if (!relation.equals("丈夫")) {
                        addToHistory(relation);
                    }
                } else {
                    //女，不能选择添加妻,
                    if (!relation.equals("妻子")) {
                        addToHistory(relation);
                    }
                }
            }
        } else {
            ToastUtils.showShort(R.string.long_relation_tips);
        }
    }


    private void getData(String content) {
        StkResApi.relationship2Called(this, content, StkRelationshipLocale.DEFAULT, mGender, mReverser ? 1 : 0, new IReqRetCallback<StkRelationshipBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkRelationshipBean result) {
                if (success) {
                    speaker.speak(getString(R.string.call_text1) + result.result.toString());
                    mDataBinding.tvJiao.setVisibility(View.VISIBLE);
                    mDataBinding.tvContent.setText(result.result.toString());
                } else {
                    ToastUtils.showShort(msg);
                }
            }
        });
    }

    private void shakeClick() {
        if (mHasShake) {
            VibrateUtils.vibrate(200);
        }
    }


    // 从历史栈中移除最近的一个内容，并更新显示
    private void removeFromHistory() {
        if (!historyStack.isEmpty()) {
            historyStack.pop();
            updateResult("");
        }
        mDataBinding.tvContent.setText("");
        if (historyStack.isEmpty()) {
            mDataBinding.btnLaoGong.setTextColor(Color.parseColor("#000000"));
            mDataBinding.btnLaoPo.setTextColor(Color.parseColor("#000000"));
            mDataBinding.btnLaoGong.setBackgroundResource(R.drawable.shape_white_8dp_bg);
            mDataBinding.btnLaoPo.setBackgroundResource(R.drawable.shape_white_8dp_bg);
        }
    }

}